package cn.shyouchi.wuguanjia.android.ui.event.temporary;

import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.CheckForAllUtils;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import cn.shyouchi.wuguanjia.android.widget.dialog.TimePickerDialogFragment;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AddTemporaryActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.edit_activity_name)
    EditText mEditActivityName;
    @BindView(R.id.edit_activity_person)
    EditText mEditActivityPerson;
    @BindView(R.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R.id.edit_activity_content)
    EditText mEditActivityContent;
    @BindView(R.id.edit_activity_position)
    EditText mEditActivityPosition;
    @BindView(R.id.edit_activity_phone)
    EditText mEditActivityPhone;

    EventApi mEventApi;
    String projectId;

    TemporaryEntity.ResultMessageBean entity;
    boolean isEdit = false;
    @BindView(R.id.edit_activity_person_count)
    EditText mEditActivityPersonCount;
    @BindView(R.id.edit_com_name)
    EditText mEditComName;
    @BindView(R.id.edit_com_person)
    EditText mEditComPerson;
    @BindView(R.id.edit_com_phone)
    EditText mEditComPhone;
    @BindView(R.id.activity_add_handover)
    LinearLayout mActivityAddHandover;

    @Override
    public int initContentView() {
        return R.layout.activity_add_temporary;
    }

    @Override
    public void initInjector() {
        mEventApi = new EventApi(mContext);
        projectId = getIntent().getStringExtra("projectId");
        entity = getIntent().getParcelableExtra("temporary");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        if (entity != null) {
            initEntity();
            isEdit = true;
            ToastUtil.showToast("编辑状态");
        }

    }

    private void initEntity() {
        mEditActivityName.setText(entity.getProjectname());
        mEditActivityPosition.setText(entity.getAddress());
        mEditActivityPerson.setText(entity.getProjectleader());
        mEditActivityPhone.setText(entity.getPhone());
        mEditActivityPersonCount.setText(String.valueOf(entity.getNumber()));
        mTvStartTime.setText(entity.getStartingtime());
        mTvEndTime.setText(entity.getEndtime());
        mEditActivityContent.setText(entity.getDescription());

        mEditComName.setText(entity.getOwner_company());
        mEditComPerson.setText(entity.getOwner_contacts());
        mEditComPhone.setText(entity.getOwner_phone());

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick({R.id.tv_start_time, R.id.tv_end_time})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_start_time:
                showTimePicker((TextView) view, "开始时间");
                break;
            case R.id.tv_end_time:
                showTimePicker((TextView) view, "结束时间");
                break;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add("保存");
        menuItem.setShowAsAction(2);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals("保存")) {
            generateEntityExit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void generateEntityExit() {


        if (TextUtils.isEmpty(mEditActivityName.getText())) {
            mEditActivityName.setError("标题不能为空");
            return;

        }

        if (TextUtils.isEmpty(mEditActivityPerson.getText())) {
            mEditActivityPerson.setError("负责人不能为空");
            return;
        }
        if (TextUtils.isEmpty(mEditActivityPersonCount.getText())) {
            mEditActivityPersonCount.setError("负责人数不能为空");
            return;
        }

        if (!TextUtils.isEmpty(mEditActivityPhone.getText())) {

            if (!CheckForAllUtils.isMobileNO(mEditActivityPhone.getText().toString())) {
                ToastUtil.showToast("手机号码格式错误");
                return;
            }

        }


        if (TextUtils.isEmpty(mTvStartTime.getText())) {
            ToastUtil.showToast("请选择开始时间");
            return;

        }
        if (TextUtils.isEmpty(mTvEndTime.getText())) {
            ToastUtil.showToast("请选择结束时间");
            return;
        }



        if (isEdit) {
            mEventApi.upTemporary(projectId,
                    String.valueOf(entity.getId()),
                    mEditActivityName.getText().toString(),
                    mEditActivityPerson.getText().toString(),
                    mEditActivityPersonCount.getText().toString(),
                    mTvStartTime.getText().toString(),
                    mTvEndTime.getText().toString(),
                    mEditActivityContent.getText().toString(),
                    mEditActivityPhone.getText().toString(),
                    mEditActivityPosition.getText().toString(),
                    mEditComName.getText().toString(),
                    mEditComPerson.getText().toString(),
                    mEditComPhone.getText().toString())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onNext(BaseRequestResult result) {
                            EventBus.getDefault().post(new DataChangeEvent());
                            finish();
                        }
                    });

        } else {
            mEventApi.addTemporary(projectId,
                    mEditActivityName.getText().toString(),
                    mEditActivityPerson.getText().toString(),
                    mEditActivityPersonCount.getText().toString(),
                    mTvStartTime.getText().toString(),
                    mTvEndTime.getText().toString(),
                    mEditActivityContent.getText().toString(),
                    mEditActivityPhone.getText().toString(),
                    mEditActivityPosition.getText().toString(),
                    mEditComName.getText().toString(),
                    mEditComPerson.getText().toString(),
                    mEditComPhone.getText().toString()

            )
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                        @Override
                        public void onNext(BaseRequestResult result) {
                            setResult(EventTemporaryActivity.REQUEST_TYPE_ADD_SUCCESS);
                            finish();
                        }
                    });
        }

    }


    public void showTimePicker(final TextView tv, String title) {
        TimePickerDialogFragment mTimePickerDialog;
        mTimePickerDialog = new TimePickerDialogFragment(getSupportFragmentManager(), title, "");
        mTimePickerDialog.setOnTimePickedListener(new TimePickerDialogFragment.onTimePickedListener() {
            @Override
            public void onPicked(String time) {
                tv.setText(time);
                tv.setTag(1);
            }
        });
        mTimePickerDialog.showWhitDate();
    }


}
